package com.linktco.common.utils.excel.style;


import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.*;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


/**
 * 水印样式
 *
 * @author zhangnx
 */
public class WaterStyle {


    /**
     * 创建水印图片
     *
     * @param waterMark 水印文字
     */
    public static BufferedImage createWatermarkImage(String waterMark, String color, Integer waterSize) {
        String[] textArray = waterMark.split("\n");
        Font font = new Font("宋体", Font.ITALIC, waterSize);
        int width = 250;
        int height = 200;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 背景透明 开始
        Graphics2D g = image.createGraphics();
        image = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        g.dispose();
        // 背景透明 结束
        g = image.createGraphics();
        // 设定画笔颜色
        g.setColor(Color.decode(color));
        // 设置画笔字体
        g.setFont(font);
        // 设定倾斜度
        g.shear(-0.5, 0);
        //设置字体平滑
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //文字从中心开始输入，算出文字宽度，左移动一半的宽度，即居中
        FontMetrics fontMetrics = g.getFontMetrics(font);

        // 水印位置
        int x = width / 2;
        int y = height / 2;
        // 设置水印旋转
        g.rotate(Math.toRadians(-40), x, y);
        for (String s : textArray) {
            // 文字宽度
            int textWidth = fontMetrics.stringWidth(s);
            // 画出字符串
            g.drawString(s, x - (textWidth / 2), y);
            y = y + font.getSize();
        }
        // 释放画笔
        g.dispose();
        return image;
    }


    /**
     * 为Excel打上水印工具函数
     * 请自行确保参数值，以保证水印图片之间不会覆盖。
     * 在计算水印的位置的时候，并没有考虑到单元格合并的情况，请注意
     *
     * @param wb              Excel Workbook
     * @param sheet           需要打水印的Excel
     * @param bufferImg 水印图片
     * @throws IOException
     */

    /**
     * @param wb
     * @param sheet
     * @param bufferImg
     * @param rowSize
     * @param cellSize
     * @param waterSpace
     */
    @SneakyThrows
    public static void putWaterRemarkToExcel(Workbook wb, Sheet sheet, BufferedImage bufferImg, Integer rowSize, Integer cellSize, Integer waterSpace) {

        //加载图片
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

        if (null == bufferImg) {
            throw new RuntimeException("向Excel上面打印水印，读取水印图片失败(2)。");
        }
        ImageIO.write(bufferImg, "png", byteArrayOut);

        //开始打水印
        Drawing drawing = sheet.createDrawingPatriarch();

        int limit = 20 - (waterSpace * 5) >= 5 ? 20 - (waterSpace * 5) : 5;
        for (int i = 10; i < rowSize; i += limit) {
            int b = 1 - Math.abs(2 * waterSpace);
            int abs = Math.abs(5 - waterSpace);
            for (int j = 0; j < cellSize; j += (abs <= 0 ? 1 : abs)) {
                if (b + j >= cellSize) {
                    continue;
                }
                ClientAnchor anchor = drawing.createAnchor(0, 0, 255, 255, b + j, i, 0, 0);
                Picture pic = drawing.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), Workbook.PICTURE_TYPE_PNG));
                pic.resize();
            }
        }
    }


}
